home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1991: Code Warrior / bincue / Code Warrior.bin / Developer Essentials / DTS Sample Code / Snippets / Snippets May '91 / Heap Purge dcmd / hp.c < prev   
Encoding:
Text File  |  1991-09-09  |  4.8 KB  |  174 lines  |  [TEXT/MPS ]

  1. -----------
  2. File: HP.c
  3. -----------
  4.  
  5. /*  HP.c
  6.   This is the Heap Purge dcmd.
  7.  
  8.   The following MPW commands will build the dcmd and copy it to the
  9.   "Debugger Prefs" file in the System folder. The dcmd's name in
  10.   MacsBug will be the name of the file built by the Linker.
  11.   You must first copy dcmd.h, dcmdGlue.a.o and DRunTime.o from the
  12.   C Samples folder into this folder.
  13.  
  14.   Asm hp.a
  15.   C -r -b hp.c #-sym on,novars,notypes -mbg off # for UltraSlimFast
  16.   Link dcmdGlue.a.o hp.c.o hp.a.o put.c.o DRuntime.o "{Libraries}"Interface.o 
  17.     -o hp
  18.   BuildDcmd hp 1235
  19.   #Echo 'include "hp";'  |  Rez -a -o "{SystemFolder}TMON Folder:DCMD Holder"
  20.   Echo 'include "hp";'   |  Rez -a -o "{SystemFolder}Debugger Prefs"
  21.  
  22.   DumpObj hp.c.o > hp.dumpObj
  23.   UltraSlimFast hp.dumpObj >hp.UltraSlimFast
  24.  
  25. */
  26.  
  27. #include <Types.h>
  28. #include <Memory.h>
  29. #include <OSUtils.h>
  30. #include <Files.h>
  31. #include <Menus.h>
  32. #include <Traps.h>
  33.  
  34. #include "dcmd.h"
  35. #include "put.h"
  36.  
  37. extern pascal void NewNewPtr();
  38. extern pascal void NewNewHandle();
  39. extern pascal void NewReallocHandle();
  40. extern pascal void NewSetPtrSize();
  41. extern pascal void NewSetHandleSize();
  42. extern pascal void NewMoveHHi();
  43. extern pascal void SaveOldTrapAddress (long address, short addressKind);
  44. extern pascal void SaveMyA5();
  45. extern pascal long GetMyA5();
  46.  
  47. #define kOldNewPtr           0
  48. #define kOldNewHandle        1
  49. #define kOldReallocHandle    2
  50. #define kOldSetPtrSize       3
  51. #define kOldSetHandleSize    4
  52. #define kOldMoveHHi          5
  53.  
  54. #define sysZoneMask          0x0400
  55.  
  56. Str255        pDumpString;
  57. char          ch;
  58. Boolean       purgingOn;
  59.  
  60.  
  61. pascal void PurgeIt(long blockAddress, long blockLength, long addrOfMasterPtr,
  62.                     short blockType, Boolean locked, Boolean purgeable, 
  63.                     Boolean resource)
  64. {
  65. #pragma unused (blockAddress, blockLength, addrOfMasterPtr, resource)
  66.  
  67.     if ((blockType == relocatableBlock) && (!locked) && (purgeable)) {
  68.         EmptyHandle((Handle)addrOfMasterPtr);
  69.     }
  70. }
  71.  
  72.  
  73. Boolean    IsSysZone(unsigned short TrapWord)
  74. {
  75.     return( (TrapWord & sysZoneMask) != 0);
  76. }
  77.  
  78.  
  79. pascal void PurgeAllBlocks(unsigned short TrapWord)
  80. {
  81.     THz     oldZone;
  82.     long    oldA5;
  83.     
  84.     oldA5 = SetA5(GetMyA5());
  85.     if (purgingOn) {
  86.         if (IsSysZone(TrapWord)) {
  87.             dcmdSwapWorlds();
  88.             oldZone = GetZone();
  89.             SetZone(SystemZone());
  90.             dcmdSwapWorlds();
  91.         }
  92.     
  93.         dcmdForAllHeapBlocks(PurgeIt);
  94.     
  95.         if (IsSysZone(TrapWord)) {
  96.             dcmdSwapWorlds();
  97.             SetZone(oldZone);
  98.             dcmdSwapWorlds();
  99.         }
  100.     }
  101.     (void) SetA5(oldA5);
  102. }
  103.  
  104. TrapType GetTrapType(short theTrap)
  105. {
  106.     // OS traps start with A0, Tool with A8 or AA. 
  107.     return((theTrap & 0x0800) ? ToolTrap : OSTrap);
  108. }
  109.  
  110. void PatchTrap(short trapNumber, short saveOffset, long newAddress)
  111. {
  112.     // Use NGetTrapAddress since it is always safer on current machines. Take 
  113.     // the result it gives me, and save it off in asm land, for future 
  114.     // reference. Then, move in the new address of the routine, my asm glue. 
  115.     
  116.     SaveOldTrapAddress(NGetTrapAddress(trapNumber, GetTrapType(trapNumber)), 
  117.                        saveOffset);
  118.     NSetTrapAddress(newAddress, trapNumber, OSTrap);    
  119. }
  120.  
  121. void InstallPatches()
  122. {
  123.     // Patch the traps… These are being patched in the world, not in the 
  124.     // debugger world. Switch over to the real world, in case the debugger 
  125.     // does world swaps. TMon Pro.
  126.  
  127.     dcmdSwapWorlds();
  128.  
  129.     PatchTrap(_NewPtr, kOldNewPtr, (long) NewNewPtr);
  130.     PatchTrap(_NewHandle, kOldNewHandle, (long) NewNewHandle);
  131.     PatchTrap(_ReallocHandle, kOldReallocHandle, (long) NewReallocHandle);
  132.     PatchTrap(_SetPtrSize, kOldSetPtrSize, (long) NewSetPtrSize);
  133.     PatchTrap(_SetHandleSize, kOldSetHandleSize, (long) NewSetHandleSize);
  134.     PatchTrap(_MoveHHi, kOldMoveHHi, (long) NewMoveHHi);
  135.  
  136.     // Switch back to debugger world.
  137.     dcmdSwapWorlds();
  138. }
  139.  
  140.  
  141. pascal void CommandEntry(dcmdBlock* paramPtr)
  142. {
  143.     switch (paramPtr->request)
  144.         {
  145.         case dcmdInit:
  146.             SaveMyA5();
  147.             purgingOn = false;
  148.             InstallPatches();
  149.             break;
  150.  
  151.         case dcmdHelp:
  152.             dcmdDrawLine("\php");
  153.             dcmdDrawLine("\p   Toggle Heap Purge on and off. When on, heap purge purges purgable blocks");
  154.             dcmdDrawLine("\p   on NewPtr, NewHandle, ReallocHandle, SetPtrSize, SetHandleSize, and MoveHHi");
  155.             dcmdDrawLine("\p   calls.");
  156.             break;
  157.  
  158.         case dcmdDoIt:
  159.             dcmdDrawLine("\pHeap purge is ");
  160.             if (purgingOn)
  161.                 dcmdDrawString("\poff.");
  162.             else
  163.                 dcmdDrawString("\pon.");
  164.             purgingOn = !purgingOn;
  165.             break;
  166.  
  167.         default:
  168.             PutPStr("\pUnknown request ");
  169.             PutUDec(paramPtr->request);
  170.             PutLine();
  171.             break;
  172.         }
  173. } // CommandEntry
  174.